%macro do_merge;

	proc printto log = "&logdir/do_merge.txt" new;
	run;	
	/************************************************
	*												*
	*		Step 1: Aggregate info returns			*
	*				to the tax unit level			*
	*												*
	************************************************/
	
	/* make duplicates of filing status */
	data dups;
		set rlib.filing_status;
		do prim = 0 to 1;
			if prim = 1 then do;
				person_id = prim_tin;
				output;
			end;
			if prim = 0 & sec_tin > 0 then do;
				person_id = sec_tin;
				output;
			end;
		end;
	run;
	proc sort data = dups(keep=person_id prim_tin sec_tin) nodupkey;
		by person_id prim_tin sec_tin;
	run;
	
	
	/* merge in info returns */
	data merged;
		merge dups rlib.info_returns;
		by person_id;
	run;
	
	/* aggregate over the couple */
	proc means data = merged nway noprint;
		class prim_tin sec_tin;
		output out = agg_info_returns(drop=_type_ _freq_)
			sum(qdiv intrec stcg ltcg
				otherprop gambling_incm f1099g_ref
				pensions ui gssi nec f1065_ord f1120s_ord 
				withholding div_199a f1065_royal f1120s_royal
				sl_int dist_sav_cred tot_contrib)
			= qdiv intrec stcg ltcg
				otherprop gambling_incm f1099g_ref
				pensions ui gssi nec f1065_ord f1120s_ord 
				withholding div_199a f1065_royal f1120s_royal
				sl_int dist_sav_cred tot_contrib
			max(early_roth t_unkn) = early_roth t_unkn;
	run;
	
	
	/* for the things that require split by couple */
	proc sort data = rlib.filing_status(keep=prim_tin sec_tin)
		out = base nodupkey;
		by prim_tin sec_tin;
	run;
	
	
	proc sql noprint;
	create table info_returns_split as
	select
		a.prim_tin,
		a.sec_tin,
		b.wgs as wages_prim,
		c.wgs as wages_sec,
		b.f1065_gp as f1065_gp_prim,
		c.f1065_gp as f1065_gp_sec,
		b.businc as businc_prim,
		c.businc as businc_sec
	from base as a
		left outer join rlib.info_returns as b
			on(a.prim_tin = b.person_id)
		left outer join rlib.info_returns as c
			on(a.sec_tin = c.person_id)
	order by a.prim_tin, 
		a.sec_tin;
	quit;
	
	
	/************************************************
	*		Step 2: Merge in the tax return info	*
	************************************************/
	
	proc sort data = rlib.filing_status(keep = prim_tin sec_tin prim_ssn sec_ssn 
		nonfiler sex_nonfiler yob_nonfiler nonfiler_x tu_match)
		out = tomerge nodupkey;
		by prim_tin sec_tin;
	run;
	
	/* All of these datasets should already be unique by prim_tin, sec_tin.
		Verify! */
	proc sort data = agg_info_returns nodupkey;
		by prim_tin sec_tin;
	run;
	
	proc sort data = info_returns_split nodupkey;
		by prim_tin sec_tin;
	run;
	proc sort data = rlib.tax_returns nodupkey;
		by prim_tin sec_tin;
	run;	
	
	data result;
		merge tomerge agg_info_returns
			info_returns_split
			rlib.tax_returns;
		by prim_tin sec_tin;
		
		
		
		/* reset missings to zero */
		array nums[*] _numeric_;
		do j = 1 to dim(nums);
			if nums(j) = . then nums(j) = 0;
		end;
		
		
		ltcg = ltcg - lt_loss_cf;
		stcg = stcg - st_loss_cf;
		
		sav_cred_base = max(0, tot_contrib - dist_sav_cred);
		
		nonprop = gambling_incm + min(st_incm_tax_max, f1099g_ref);
		drop gambling_incm st_incm_tax_max f1099g_ref j
			dist_sav_cred tot_contrib;
		
	run;
	
	/*
	merge in the extra dependents 
	*/
	data rlib.result;
		merge result(in=a) rlib.extra_deps(in=b);
		by prim_tin;
		if a;
		array ba{4} extra_deps extra_deps13 extra_deps17 extra_deps18;
		do i = 1 to 4;
			if ba(i) = . then ba(i) = 0;
		end;
		drop i;
	run;
	
	proc print data = rlib.result(obs=5);
	run;
	
	
	
	proc printto;
	run;
	
	
%mend;